﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KingCat_DataBase;
using KingCat_Socket;

namespace KingCat_Client
{
    public partial class Login : Form
    {
        public static string userName;
        public Login()
        {
            InitializeComponent();
            this.label5.Visible = false;
            this.comboBox2.SelectedIndex = 0;
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Visible = false;
            new Register().ShowDialog();
        }


        /// <summary>
        /// 处理用户登入功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            label5.Visible = false;
            //向服务器发送用户名跟密码
            userName = comboBox1.Text;
            string msg = "用户登入|" + comboBox1.Text + "|" + textBox1.Text;
            //注意当向服务器发送数据时，一定要刷新一下缓冲流，否则系统可能将数据存在缓冲区而一时并没有发送出去
            ConnectServerSocket.getInstance().getStreamWriter().WriteLine(msg);
            ConnectServerSocket.getInstance().getStreamWriter().Flush();
            string result = ConnectServerSocket.getInstance().getStreamReader().ReadLine();
            if (result == "true")
            {
                this.Visible = false;
                new ChatRoom().ShowDialog();
            }
            else
            {
                label5.Visible = true;
                label5.ForeColor = Color.Red;
            }
        }
    }
}